﻿// -----------------------------------------------------------------------
// <copyright file="Colour.cs" company="Image Processing Course Project">
// Created 31/10/2011
// </copyright>
// -----------------------------------------------------------------------

namespace ImageProcessing.IntermediateClasses
{
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using ImageProcessing.Utilities;

    /// <summary>
    /// Class of a struct to carry RGB of a color
    /// </summary>
    public class Colour
    {
        /// <summary>
        /// Converts a bitmap image to a ColorsStruct 2D Array
        /// </summary>
        /// <param name="image">Bitmap Image</param>
        /// <returns>ColorsStruct 2D Array</returns>
        public static ColorsStruct[,] ConvertToStruct(Bitmap image)
        {
            ColorsStruct[,] newImage = new ColorsStruct[image.Width, image.Height];
            try
            {
                for (int i = 0; i < image.Height; i++)
                {
                    for (int j = 0; j < image.Width; j++)
                    {
                        newImage[j, i].Red = image.GetPixel(j, i).R;
                        newImage[j, i].Green = image.GetPixel(j, i).G;
                        newImage[j, i].Blue = image.GetPixel(j, i).B;
                    }
                }

                return newImage;
            }
            catch (Exception ex)
            {
                Logger.LogException(ex);
                throw;
            }
        }

        /// <summary>
        /// Structure of integer values representing colors
        /// </summary>
        public struct ColorsStruct
        {
            /// <summary>
            /// Red value of image pixel
            /// </summary>
            public int Red;

            /// <summary>
            /// Green value of image pixel
            /// </summary>
            public int Green;

            /// <summary>
            /// Blue value of image pixel
            /// </summary>
            public int Blue;
        }

        public Bitmap BinaryImage(Bitmap image, int threshold)
        {
            for (int i = 0; i < image.Height; i++)
            {
                for (int j = 0; j < image.Width; j++)
                {
                    int temp = (int)image.GetPixel(j, i).R;
                    if (temp < threshold)
                    {
                        image.SetPixel(j, i, Color.FromArgb(0, 0, 0));
                    }
                    else
                    {
                        image.SetPixel(j, i, Color.FromArgb(255, 255, 255));
                    }
                }
            }
            return image;
        }
    }
}
